package cn.edu.seu.cose.qol.ws.app;

import org.restlet.Application;
import org.restlet.Restlet;
import org.restlet.routing.Router;

import cn.edu.seu.cose.qol.ws.resource.admin.AdminInstanceResource;
import cn.edu.seu.cose.qol.ws.resource.admin.AdminsResource;
import cn.edu.seu.cose.qol.ws.resource.questionnaire.QuestionBaseInstanceResource;
import cn.edu.seu.cose.qol.ws.resource.questionnaire.QuestionResource;
import cn.edu.seu.cose.qol.ws.resource.questionnaire.QuestionnaireResource;
import cn.edu.seu.cose.qol.ws.resource.questionnaire.QuestionnairesResource;
import cn.edu.seu.cose.qol.ws.resource.root.RootResource;
import cn.edu.seu.cose.qol.ws.resource.template.TemplatesResource;

public class QOLApplication extends Application
		implements RequestAttributes {
	
	private static final String ROOT_PATH = "";
	
	private static final String QUESTIONNAIRES_PATH =
			"/questionnaires";
	
	private static final String QUESTIONNAIRE_RESOURCE_PATH =
			QUESTIONNAIRES_PATH + "/{" + QUESTIONNAIRE_NAME + "}";
	
	private static final String QUESTION_BASE_INSTANCE_RESOURCE_PATH =
			QUESTIONNAIRE_RESOURCE_PATH + "/{" + QUESTION_BASE_NAME + "}";
	
	private static final String QUESTION_RESOURCE_PATH =
			QUESTION_BASE_INSTANCE_RESOURCE_PATH + "/{" + QUESTION_NO + "}";
	
	private static final String ADMINS_RESOURCE_PATH =
			"/admins";
	
	private static final String ADMIN_INSTANCE_RESOURCE_PATH =
			"/admins/{" + ADMIN_NAME + "}";
	
	private static final String TEMPLATES_RESOURCE_PATH =
			"/templates";
	
	private Router _router = new Router(getContext());
	
	public QOLApplication() {}
	
	@Override
	public synchronized Restlet createInboundRoot() {
		addQuestionnaireResourceToRouter();
		addAdminResourceToRouter();
		addTemplateResourceToRouter();
		addRootResourceToRouter();
		return _router;
	}
	
	private void addRootResourceToRouter() {
		_router.attach(ROOT_PATH, RootResource.class);
	}
	
	private void addQuestionnaireResourceToRouter() {
		_router.attach(QUESTIONNAIRES_PATH,
				QuestionnairesResource.class);
		_router.attach(QUESTIONNAIRE_RESOURCE_PATH,
				QuestionnaireResource.class);
		_router.attach(QUESTION_BASE_INSTANCE_RESOURCE_PATH,
				QuestionBaseInstanceResource.class);
		_router.attach(QUESTION_RESOURCE_PATH,
				QuestionResource.class);
	}
	
	private void addAdminResourceToRouter() {
		_router.attach(ADMINS_RESOURCE_PATH,
				AdminsResource.class);
		_router.attach(ADMIN_INSTANCE_RESOURCE_PATH,
				AdminInstanceResource.class);
	}
	
	private void addTemplateResourceToRouter() {
		_router.attach(TEMPLATES_RESOURCE_PATH,
				TemplatesResource.class);
	}

}
